/**
 * 
 */
package com.Adapters;


import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.Adapters.Items.ActionsUpload;
import com.Adapters.Items.ItemPost;
import com.Adapters.Items.ItemUpload;
import com.Data.Post;
import com.Init.ImageDownloaderInit;
import com.nostra13.universalimageloader.core.ImageLoader;


/**
 * @author Chris
 * 
 */
public class UploadAdapter extends ArrayAdapter<Post> {

    public ArrayList<ItemPost> getRows() {

        return this.rows;
    }

    private int column;

    private ActionsUpload main;

    private ArrayList<ItemPost> rows;

    private ImageLoader loader;

    /**
     * @param context
     * @param textViewResourceId
     */
    public UploadAdapter(Context context, int textViewResourceId,
                int column, ActionsUpload main) {

        super(context, textViewResourceId);
        rows = new ArrayList<ItemPost>();
        this.column = column;
        this.main = main;

        setOptions();
    }

    private void setOptions() {

        ImageDownloaderInit.config(getContext(), ".upload/");
        this.loader = ImageLoader.getInstance();

    }

    @Override
    public void add(Post object) {

        rows.add(new ItemUpload(column, LayoutInflater.from(getContext()), object, main, loader));
    }

    @Override
    public int getCount() {

        return rows.size();
    }

    @Override
    public Post getItem(int position) {

        return ((ItemUpload) rows.get(position)).getItemup();
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    public ItemUpload getUploadItem(int position) {

        return (ItemUpload) rows.get(position);
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {

        return rows.get(position).getView(v, position);

    }

    public Integer removeItem(Post object) {

        for (int i = 0; i < rows.size(); i++) {
            ItemPost p = rows.get(i);
            if (object.getID() == p.getPostId()) {
                rows.remove(i);

                notifyDataSetChanged();

                return i;
            }
        }
        return 0;
    }

}
